<?php


namespace app\admin\controller\mall;


use app\admin\traits\Curd;
use app\common\controller\AdminController;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\model\MallGoods;
use app\common\model\MallGoodsMendian;
use think\App;
use think\Model;

class Mendian extends AdminController
{
    use Curd;
    protected $relationSearch = true;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new MallGoodsMendian();
    }

    public function index($id=''){
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $where['goods_id']=$id;
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            foreach ($list as $k=>$v){
                $goods =(new MallGoods())->where("id='".$v['goods_id']."'")->find();
                $list[$k]['goods_name'] =$goods['title'];
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $this->assign('gid',$id);
        return $this->fetch();
    }


    /**
     * @NodeAnotation(title="添加")
     */
    public function add($id='')
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $post['goods_id'] =$id;
                $weizhi =explode(',',$post['store_str']);
                $post['store_lat'] =$weizhi[0];
                $post['store_lng'] =$weizhi[1];
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            // 更新商品门店字段
            $mendian=$this->model->field('store_title,store_name,store_mobile,store_lat,store_lng,store_date')->where("goods_id='$id'")->select()->toArray();
            if($mendian){
                (new MallGoods())->where("id='$id'")->save(['mendian'=>json_encode($mendian)]);
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }

        $this->assign('gid',$id);
        return $this->fetch();
    }


    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {

                $weizhi =explode(',',$post['store_str']);
                $post['store_lat'] =$weizhi[0];
                $post['store_lng'] =$weizhi[1];
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            // 更新商品门店字段
            $mendian=$this->model->field('store_title,store_name,store_mobile,store_lat,store_lng,store_date')->where("goods_id='".$row['goods_id']."'")->select()->toArray();
            if($mendian){
                (new MallGoods())->where("id='".$row['goods_id']."'")->save(['mendian'=>json_encode($mendian)]);
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }



}